﻿using System.Collections.Generic;
using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_RelatedProducts : WebControl
    {
        private Database database = DatabaseFactory.CreateDatabase();
        public wnxd_RelatedProducts()
        {
            base.ID = "wnxd_RelatedProducts";
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }
        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            DataTable info = this.GetRelatedProducts(this.productId, this.MaxNum);
            foreach (DataRow row in info.Rows)
            {
                builder.Append("<div class=\"right-pro\">");
                string title = string.Empty;
                string alt = string.Empty;
                string keyword = row["Meta_Keywords"].ToString();
                if (!string.IsNullOrEmpty(keyword))
                {
                    string[] arr = keyword.Split('|');
                    if (arr.Length == 3)
                    {
                        title = arr[1];
                        alt = arr[2];
                    }
                }
                builder.AppendFormat("<a href=\"{1}\"><img src=\"{0}\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                builder.AppendFormat("<p><a title=\"{0}\" href=\"{1}\" style=\"color:#333333\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                if (row["SalePrice"] == DBNull.Value) row["SalePrice"] = row["MarketPrice"];
                builder.AppendFormat("</p><b>￥{0}</b>", Globals.FormatMoney((decimal)row["SalePrice"]));
                builder.Append("</div>");
            }
            return builder.ToString();
        }
        private DataTable sql_call(string sql)
        {
            using (DbCommand cmd = database.GetSqlStringCommand(sql))
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                return DataHelper.ConverDataReaderToDataTable(reader);
            }
        }
        private DataTable GetRelatedProducts(int productId, int maxnum)
        {
            return this.sql_call(string.Format("select top {0} * from vw_Hishop_BrowseProductList where TypeId=(select TypeId from Hishop_Products where ProductId={1}) and ProductId!={1} order by ShowSaleCounts desc", maxnum, productId));
        }
        public int productId { get; set; }
        public int MaxNum { get; set; }
    }
}
